<!-- 模块说明 -->
<script setup lang='ts'>
// @ts-ignore
import { ref, onMounted } from 'vue'
// @ts-ignore
import { areaList } from '@vant/area-data';
// @ts-ignore
import { Address } from '../../api';
import { IAddress } from '../../api/address';
// @ts-ignore
import { showSuccessToast, showFailToast } from 'vant';
// @ts-ignore
import { useRoute, useRouter } from 'vue-router';
// @ts-ignore
import { useCustomersStore } from "./../../store";
// @ts-ignore
const customerstore = useCustomersStore()
const route = useRoute();
const router = useRouter();
const newAddress = ref<IAddress>({
    addressDetail: "34",
    areaCode: "515415",
    city: "54",
    county: "45",
    customer: "4",
    isDefault: 0,
    name: "45",
    province: "54",
    tel: "45"
});
const onSave = async (AddressEditInfo: any) => {//一个是默认地址点击编辑的时候，按钮要开。二是点击编辑的时候，本来不是默认的按钮，修改的时候要把布尔值变成01
    if (route.params.addrid != '') {//如果这里有参数，就说明是编辑过来的
        //编辑用户
        //这里的值如果是修改了，就是布尔值，默认为false，0。如果是true，就是1.条件里面无论是数字还是布尔值，都可以当成条件判断
        // console.log(AddressEditInfo.isDefault,"编辑点击保存按钮后");
        const copyAddressEditInfo = { ...AddressEditInfo };
        if (copyAddressEditInfo.isDefault) {
            copyAddressEditInfo.isDefault = 1
        } else {
            copyAddressEditInfo.isDefault = 0
        }
        let res = await Address['editaddr']({ ...copyAddressEditInfo, id: route.params.addrid })
        // @ts-ignore
        if (res.code == "1") {
            showSuccessToast('修改地址成功');
            router.go(-1);
        } else {
            showFailToast('修改地址失败')
        }

    } else {
        //添加用户
        console.log(AddressEditInfo.isDefault, "点击保存之后的值");
        newAddress.value = {
            addressDetail: AddressEditInfo.addressDetail,
            areaCode: AddressEditInfo.areaCode,
            city: AddressEditInfo.city,
            county: AddressEditInfo.county,
            customer: 'customerstore.id',//这里的用户id需要从本地存储拿
            isDefault: AddressEditInfo.isDefault == true ? 1 : 0,
            name: AddressEditInfo.name,
            province: AddressEditInfo.province,
            tel: AddressEditInfo.tel
        }
        let res = await Address['add'](newAddress.value)
        // @ts-ignore
        if (res.code == 1) {
            showSuccessToast('添加地址成功');
            router.go(-1);
        } else {
            showFailToast('添加地址失败')
        }
    }
};
onMounted(() => {
    if (route.params.addrid != '') { edit() }

})
const editobj = ref({})


const edit = async () => {
    console.log(route.params.addrid, "addrid的值");
    // @ts-ignore
    let res = await Address['getAaddr'](route.params.addrid)
    console.log(res.data, "要编辑的地址所有信息");
    editobj.value = res.data
    // @ts-ignore
    editobj.value.isDefault = res.data.isDefault == 0 ? false : true//怎么能让它更快速显示，不要让用户看到在滑动
}
const del = async (AddressEditInfo: any) => {
    console.log(AddressEditInfo, "点击删除")
    let res = await Address['deladdr'](AddressEditInfo.id)
    // @ts-ignore
    if (res.code = "1") {
        showSuccessToast('删除地址成功');
        router.go(-1);
    } else {
        showFailToast('删除地址失败')
    }
}
</script>
<template>
    <!-- <router-view></router-view> -->
    <van-address-edit @delete="del" show-delete :address-info="editobj" :area-list="areaList" show-set-default
        show-search-result  :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" />
</template>

<style lang="scss" scoped></style>